package com.game.manager.security;

import com.game.manager.common.GmConstants;
import com.game.manager.common.GmDateUtils;
import com.game.manager.dao.entity.GmAccount;
import com.game.manager.service.GmAccountService;
import com.game.manager.service.JwtTokenService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class GmAuthenticationManager implements AuthenticationManager{

    @Autowired
    protected JwtTokenService jwtTokenService;

    @Autowired
    protected GmAccountService accountService;
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        if(authentication.getPrincipal() == null || authentication.getCredentials() == null) {
            throw new BadCredentialsException("登陆验证失败,用户名或密码为空");
        }
        String username = (String)authentication.getPrincipal();
        String password = (String)authentication.getCredentials();
        //从数据库获取账户信息
        GmAccount gmAccount = accountService.getGmAccount(username);
        if((gmAccount == null)&&(username.equals(GmConstants.ADMIN))) {
            gmAccount = new GmAccount();
            List<String> roles = new ArrayList<>();
            roles.add("73e95060f3234011ad04f2c551342cac");
            gmAccount.setCreateTime(GmDateUtils.yyyyMMddHHmmss());
            gmAccount.setCreator(username);
            gmAccount.setUsername(username);
            gmAccount.setPassword(password);
            gmAccount.setRoles(roles);
            accountService.updateAccountRole(gmAccount);
        }

        if(!gmAccount.getPassword().equals(password)) {
            throw new BadCredentialsException("用户或密码错误");
        }
        log.info("{} 登陆成功",username);
        //这里添加权限
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        List<String> authoritiesString = authorities.stream()
                .map(GrantedAuthority::getAuthority)
                .collect(Collectors.toList());
        String token = jwtTokenService.createToken(username, authoritiesString);
        authoritiesString.add(token);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password, authorities);
        authenticationToken.setDetails(gmAccount);
        return authenticationToken;
    }
}
